<template>
  <div id="userLogin">
    <a-form
      :model="form"
      label-align="left"
      :style="{ width: '600px' }"
      @submit="handleSubmit"
      @register="handleRegister"
      style="margin: 0 auto"
    >
      <a-form-item field="userAccount" tooltip="请输入你的账号" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" style="width: 100px" type="primary"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
    <div style="margin-right: 250px">
      <a href="/user/register">没有账号？</a>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const router = useRouter();
const store = useStore();
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    //更新用户状态
    await store.dispatch("user/getLoginUser");
    //登录成功跳转到首页
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败," + res.message);
  }
};
</script>
